package com.tongtailian.www.stir.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tongtailian.www.stir.R;
import com.tongtailian.www.stir.bean.CleanConfigBean;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by liuml on 2020/9/6 16:48
 */
public class ConfigEditAdapter extends BaseQuickAdapter<CleanConfigBean, BaseViewHolder> {

    private boolean isEdit = false;

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public SaveEditListener saveEditListener;

    public interface SaveEditListener {

        void SaveEdit(int position, String string);

        void SaveEdit2(int position, String string);
    }


    public void setSaveEditListener(SaveEditListener saveEditListener) {
        this.saveEditListener = saveEditListener;
    }

    public ConfigEditAdapter(int layoutResId, @Nullable List<CleanConfigBean> data) {
        super(layoutResId, data);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    protected void convert(@NonNull final BaseViewHolder helper, final CleanConfigBean item) {
        /**
         *
         * btItemName = (LinearLayout) findViewById(R.id.bt_item_name);
         * tvName = (TextView) findViewById(R.id.tv_name);
         * tvValue = (TextView) findViewById(R.id.tv_value);
         * etInput = (EditText) findViewById(R.id.et_input);
         * tvUnit = (TextView) findViewById(R.id.tv_unit);
         */
        final int adapterPosition = helper.getAdapterPosition();
        final TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null && saveEditListener != null) {
                    EditText et = helper.getView(R.id.et_input);
                    String s1 = et.getText().toString();
                    saveEditListener.SaveEdit(adapterPosition, s1);
                }
            }
        };
        final TextWatcher watcher2 = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (item.getStyleType() == 2) {
                    if (s != null && saveEditListener != null) {
                        EditText et = helper.getView(R.id.et_input1);
                        String s1 = et.getText().toString();
                        saveEditListener.SaveEdit2(adapterPosition, s1);
                    }
                }


            }
        };
        if (item.getStyleType() == 1) {
            helper.setGone(R.id.ll_type1, true)
                    .setGone(R.id.ll_type2, false)
                    .setGone(R.id.iv_type3, false);
        } else if (item.getStyleType() == 2) {
            helper.setGone(R.id.ll_type1, true)
                    .setGone(R.id.ll_type2, true)
                    .setGone(R.id.iv_type3, false);
        } else if (item.getStyleType() == 3) {
            helper.setGone(R.id.ll_type1, false)
                    .setGone(R.id.ll_type2, false)
                    .setGone(R.id.iv_type3, true);
            helper.addOnClickListener(R.id.iv_type3);
        }
        EditText editText = helper.getView(R.id.et_input);
        EditText editText1 = helper.getView(R.id.et_input1);
        editText.setHint("名称限制8个字");

//        editText.addTextChangedListener(watcher);
//        editText1.addTextChangedListener(watcher2);

        helper.setText(R.id.tv_name, item.getLeftName())
                .setText(R.id.et_input, item.getValue())
                .setText(R.id.tv_unit, item.getUnit())
                .setText(R.id.et_input1, item.getValue2())
//                .setGone(R.id.et_input, true)
                .setGone(R.id.tv_value, false);

        if ("1".equals(item.getIsCheckd())) {
            helper.setBackgroundRes(R.id.iv_type3, R.mipmap.check);
        } else {
            helper.setBackgroundRes(R.id.iv_type3, R.mipmap.check_none);
        }


    }
}